package org.eiahe.hr.performance.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.eiahe.hr.performance.domain.PerformancePositionIndicator;
import org.eiahe.hr.performance.domain.bo.PerformancePositionIndicatorBo;
import org.eiahe.hr.performance.domain.bo.PerformancePositionIndicatorQueryBo;
import org.eiahe.hr.performance.domain.vo.PerformancePositionIndicatorVo;
import org.eiahe.hr.performance.service.IPerformancePositionIndicatorService;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import java.math.BigDecimal;
import java.util.List;

/**
 * 岗位指标关联Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/performance/positionIndicator")
public class PerformancePositionIndicatorController extends BaseController {

    private final IPerformancePositionIndicatorService performancePositionIndicatorService;

    /**
     * 查询岗位指标关联列表
     */
    @SaCheckPermission("performance:positionIndicator:list")
    @GetMapping("/list")
    public TableDataInfo<PerformancePositionIndicatorVo> list(PerformancePositionIndicatorQueryBo queryBo,
            PageQuery pageQuery) {
        return performancePositionIndicatorService.queryPageListVoByCondition(queryBo, pageQuery);
    }

    /**
     * 查询岗位指标树状结构
     */
    @SaCheckPermission("performance:positionIndicator:list")
    @GetMapping("/tree/{positionId}")
    public R<List<PerformancePositionIndicatorVo>> listTree(@PathVariable("positionId") Long positionId) {
        List<PerformancePositionIndicatorVo> tree = performancePositionIndicatorService
                .getPositionIndicatorTree(positionId);
        return R.ok(tree);
    }

    /**
     * 查询岗位指标权重
     */
    @SaCheckPermission("performance:positionIndicator:list")
    @GetMapping("/weight")
    public R<BigDecimal> getIndicatorWeight(
            @RequestParam Long positionId,
            @RequestParam Long indicatorId) {
        BigDecimal weight = performancePositionIndicatorService
                .getPositionIndicatorWeight(positionId, indicatorId);
        return R.ok(weight);
    }

    /**
     * 获取岗位指标关联详细信息
     */
    @SaCheckPermission("performance:positionIndicator:query")
    @GetMapping(value = "/{id}")
    public R<PerformancePositionIndicatorVo> getInfo(@PathVariable("id") Long id) {
        return R.ok(performancePositionIndicatorService.queryById(id));
    }

    /**
     * 新增岗位指标关联
     */
    @SaCheckPermission("performance:positionIndicator:add")
    @Log(title = "岗位指标关联", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody PerformancePositionIndicatorBo indicator) {
        return toAjax(performancePositionIndicatorService.insertByBo(indicator));
    }

    /**
     * 修改岗位指标关联
     */
    @SaCheckPermission("performance:positionIndicator:edit")
    @Log(title = "岗位指标关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody PerformancePositionIndicatorBo indicator) {
        return toAjax(performancePositionIndicatorService.updateByBo(indicator));
    }

    /**
     * 删除岗位指标关联
     */
    @SaCheckPermission("performance:positionIndicator:remove")
    @Log(title = "岗位指标关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(performancePositionIndicatorService.deleteWithValidByIds(List.of(ids), true));
    }
}